home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 081 / strp_all.arc / STRP_ALL.C next >
Encoding:
C/C++ Source or Header  |  1988-04-02  |  7.2 KB  |  224 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*               The Opus Computer-Based Conversation System                */
  3. /*       (c) Copyright 1986, Wynn Wagner III, All Rights Reserved           */
  4. /*--------------------------------------------------------------------------*/
  5. /*                                                                          */
  6. /* STRIP_Z: Filter Control-Z (CP/M style end-of-file marker) from text file */
  7. /* Renamed STRP_ALL                                                         */
  8. /* COMPILER: MicroSoft C, v4.0                                              */
  9. /*  Modified 4/2/88 by J.D Bowman (135/38) to strip any ascii char.         */
  10. /* Translated back into the C language from Cpascal on the same date <smile> */
  11. /*--------------------------------------------------------------------------*/
  12.  
  13. #include <io.h>
  14. #include <stdio.h>
  15. #include <types.h>
  16. #include <stat.h>
  17. #include <malloc.h>
  18.  
  19. static char *BUFFER_ERROR = "\r\nFile buffer error. Unable to continue.\r\n$";
  20.  
  21.  
  22.  
  23.  
  24. /*--------------------------------------------------------------------------*/
  25. /* EXTENDED HELP                                                            */
  26. /*--------------------------------------------------------------------------*/
  27. void extended_help()
  28. {
  29.       bdos(9,"Strp_ALL removes any single character from a test file.\r\n$");
  30.        bdos(9,"First, it creates a backup file (`.BAK') of the program...\r\n$");
  31.        bdos(9,"then does the filtering.\r\n\n$");
  32.        bdos(9,"      USAGE:      strp_all filename decimal_char\r\n\n$");
  33.        bdos(9,"      EXAMPLE:    strp_z c:\\pascal\\files.bbs 12\r\n\n$");
  34.       bdos(9,"      will strip all formfeeds from files.bbs .\r\n\n$");
  35.       bdos(9,"      If no character is given on the command line, it is\r\n\n$");
  36.       bdos(9,"      assumed you wish to strip the cpm eof (decimal 26).\r\n\n$");
  37.  
  38.       exit(1);
  39.  
  40. }
  41.  
  42. /*--------------------------------------------------------------------------*/
  43. /* STRMFE: Make File Extension                                              */
  44. /*--------------------------------------------------------------------------*/
  45. void strmfe( new, old, extension )
  46.    char *new, *old, *extension;
  47. {
  48.       register int i;
  49.  
  50.       strcpy( new, old );
  51.  
  52.       for(i=0;i<79;i++)
  53.          {
  54.          if (!new[i])
  55.             {
  56.                new[i]   = '.';
  57.                new[i+1] = '\0';
  58.                goto done;
  59.             }
  60.                else if (new[i]=='.')
  61.                {
  62.                new[i+1] = '\0';
  63.                goto done;
  64.                }
  65.           }
  66. done:
  67.       strcat( new, extension );
  68. } /* strmfe */
  69.  
  70.  
  71.  
  72.  
  73. /*--------------------------------------------------------------------------*/
  74. /* MAIN: Strip_Z                                                            */
  75. /*--------------------------------------------------------------------------*/
  76. main(argc,argv)
  77.    int   argc;
  78.    char *argv[];
  79. {
  80.       register int   c;
  81.       register int   c1;
  82.  
  83.       FILE          *source;
  84.       FILE          *dest;
  85.  
  86.       char          *source_buffer;
  87.       char          *dest_buffer;
  88.  
  89.       char           backup[82];
  90.       struct stat   *statptr;
  91.  
  92.  
  93.  
  94.       bdos(9,"STRP_ALL[.01] Modified Strip_Z by J.D. Bowman\r\n$");
  95.       if (argc<2) extended_help();
  96.  
  97.       /*--------------------------------------------------------------------*/
  98.       /* See if the requested file exists.                                  */
  99.       /*--------------------------------------------------------------------*/
  100.       statptr  = (struct stat *)malloc( sizeof(struct stat) );
  101.       if (stat(argv[1],statptr))
  102.          {
  103.              free( statptr );
  104.             bdos(9,"\r\n\nNO SUCH FILE\r\n$");
  105.             extended_help();
  106.          }
  107.       free( statptr );
  108.  
  109.  
  110.       /*--------------------------------------------------------------------*/
  111.       /* Maintain a backup copy of the file.                                */
  112.       /*--------------------------------------------------------------------*/
  113.       strmfe( backup, argv[1], "Bak" );
  114.       unlink( backup );
  115.       if (rename(argv[1],backup))
  116.          {
  117.              bdos(9,"\r\n\nCAN'T CREATE BACKUP\r\n$");
  118.             extended_help();
  119.          }
  120.  
  121.  
  122.       /*--------------------------------------------------------------------*/
  123.       /* Open the source and destination files                              */
  124.       /*--------------------------------------------------------------------*/
  125.       source   = fopen( backup, "rb" );
  126.       if (!source)
  127.          {
  128.              bdos(9,"\r\nCAN'T OPEN SOURCE FILE\r\n$");
  129.             extended_help();
  130.          }
  131.  
  132.       dest     = fopen( argv[1], "wb" );
  133.       if (!dest)
  134.          {
  135.              bdos(9,"\r\nCAN'T OPEN DESTINATION FILE\r\n$");
  136.             extended_help();
  137.          }
  138.  
  139.  
  140.       /*--------------------------------------------------------------------*/
  141.       /* Setup some elephant buffers                                        */
  142.       /*--------------------------------------------------------------------*/
  143.       c  = _memavl() / 3;
  144.       source_buffer  = malloc(c);
  145.       dest_buffer    = malloc(c);
  146.  
  147.       if ((!source_buffer) || (!dest_buffer))
  148.          {
  149.              bdos(9,"\r\nMEMORY ERROR: unable to continue\r\n$");
  150.             exit(1);
  151.          }
  152.  
  153.       if (setvbuf(source,source_buffer,_IOFBF,c))
  154.          {
  155.              bdos(9,BUFFER_ERROR);
  156.             exit(1);
  157.          }
  158.  
  159.       if (setvbuf(dest,dest_buffer,_IOFBF,c))
  160.          {
  161.              bdos(9,BUFFER_ERROR);
  162.             exit(1);
  163.          }
  164.  
  165.  
  166.  
  167.       /*--------------------------------------------------------------------*/
  168.       /* Copy the file                                                      */
  169.       /*--------------------------------------------------------------------*/
  170.       if(argc < 3)
  171.          {
  172.           while(1)
  173.              {
  174.                    switch( c=getc(source))
  175.                       {
  176.  
  177.                           case   EOF  :  fclose(source);
  178.                                         fclose(dest);
  179.                                         free(source_buffer);
  180.                                         free(dest_buffer);
  181.                                         exit(0);
  182.  
  183.                         case   0x1a :  break;
  184.  
  185.                        default     :  putc( c, dest );
  186.  
  187.                       } /* switch */
  188.              }
  189.          }
  190.       if(argc > 2)
  191.          {
  192.          c=atoi(argv[2]);
  193.          if((c<1) || (c>255))
  194.             {
  195.             printf("Filter Character %d is out of Ascii Range (1 - 255)\n",c);
  196.             extended_help();
  197.             fclose(source);
  198.             fclose(dest);
  199.             free(source_buffer);
  200.             free(dest_buffer);
  201.             exit(1);
  202.             }
  203.          printf("Filtering ascii character %d (Hex %x)\n",c,c);
  204.          while(1)
  205.              {
  206.                     c1=getc(source);
  207.                    if(c1==EOF)
  208.                       {
  209.                       fclose(source);
  210.                       fclose(dest);
  211.                       free(source_buffer);
  212.                       free(dest_buffer);
  213.                       exit(0);
  214.                       }
  215.                    if(c1==c) continue;
  216.  
  217.                    putc( c1, dest );
  218.  
  219.              }
  220.  
  221.         } /* end if argc >2 */
  222.        
  223. }
  224.